package people.paper.tv.activity.browser;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.skyworth.ui.api.SkyWithBGLoadingView;

import java.io.File;

import people.paper.tv.activity.BaseActivity;

public class BrowserActivity extends BaseActivity {
    public static final void start(Context context, String url) {
        Intent intent = new Intent();
        intent.setClass(context, BrowserActivity.class);
        intent.setData(Uri.parse(url));
        if (!(context instanceof Activity))
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String url = parseUrl();
        if (TextUtils.isEmpty(url))
            finish();
        else {
            try {
                final FrameLayout root = new FrameLayout(this);
                root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                setContentView(root);
                webView = new WebView(this);
                webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                root.addView(webView);
                final SkyWithBGLoadingView loadingView = new SkyWithBGLoadingView(this);
                loadingView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
                loadingView.showLoading();
                root.addView(loadingView);

                webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                webView.getSettings().setLoadWithOverviewMode(true);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
                webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);  //设置 缓存模式
                // 开启 DOM storage API 功能
                webView.getSettings().setDomStorageEnabled(true);
                //开启 database storage API 功能
                webView.getSettings().setDatabaseEnabled(true);
                String cacheDirPath = new File(getCacheDir(), "webview").getAbsolutePath();
                //设置数据库缓存路径
                webView.getSettings().setDatabasePath(cacheDirPath);
                //设置  Application Caches 缓存目录
                webView.getSettings().setAppCachePath(cacheDirPath);
                //开启 Application Caches 功能
                webView.getSettings().setAppCacheEnabled(true);
                webView.setWebViewClient(new WebViewClient() {
                    @Override
                    public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(view, url);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                loadingView.dismissLoading();
                                root.removeView(loadingView);
                            }
                        });
                    }
                });
                webView.setBackgroundColor(0);
                webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
                webView.loadUrl(url);
            } catch (Exception e) {
                e.printStackTrace();
                finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        try {
            webView.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    private String parseUrl() {
        Intent intent = getIntent();
        if (intent != null) {
            Uri uri = intent.getData();
            if (uri != null) {
                return uri.toString();
            }
        }
        return null;
    }
}
